package cn.edu.xhu.library_management.area.controller;

import cn.edu.xhu.library_management.area.entity.LmBookRackArea;
import cn.edu.xhu.library_management.area.service.BookRackAreaService;
import cn.edu.xhu.library_management.common.entity.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2022 - 11 - 22
 * @Description: 书架区域Controller
 */
@Api(tags = "书架区域模块")
@ApiSort(6)
@RestController
@RequestMapping("/lm_area")
public class BookRackAreaController {
    @Autowired
    private BookRackAreaService bookRackAreaService;

    @GetMapping("/rackArea")
    @ApiOperation(value = "通过书架id，查询所有的书架区域")
    @ApiOperationSupport(order = 611)
    public R findRackAreasByRackId(Integer rackId){
        List<LmBookRackArea> rackAreas = bookRackAreaService.findRackAreasByRackId(rackId);
        return !rackAreas.isEmpty()?R.ok().data("rackAreas",rackAreas): R.error("数据为空");
    }


    @PostMapping("/rackArea")
    @ApiOperation(value = "添加书架区域")
    @ApiOperationSupport(order = 621)
    public R addRackArea(@RequestBody LmBookRackArea rackArea){
        return bookRackAreaService.addRackArea(rackArea)?R.ok("添加区域信息成功!!!"):R.error("添加区域信息失败!!!");
    }


    /**
     * 通过书架区域id，修改书架
     *
     * @param rackArea 书架区域对象
     * @return 修改成功 返回true 修改失败 返回 false
     */
    @PutMapping("/rackArea")
    @ApiOperation(value = "通过书架区域id，修改书架")
    @ApiOperationSupport(order = 631)
    public R updateRackArea(@RequestBody LmBookRackArea rackArea){
        return bookRackAreaService.updateRackArea(rackArea)?R.ok("修改区域信息成功!!!"):R.error("修改区域信息失败!!!");
    }

    /**
     * 通过书架区域id，删除书架区域
     *
     * @param rackAreaId 书架区域id
     * @return 删除成功 返回true 删除失败 返回 false
     */
    @DeleteMapping("/rackArea/{id}")
    @ApiOperation(value = "通过书架区域id，删除书架区域")
    @ApiOperationSupport(order = 641)
    public R deleteRackAreaById(@PathVariable("id") Integer rackAreaId){
        return bookRackAreaService.deleteRackAreaById(rackAreaId) ? R.ok("删除区域信息成功!!!"):R.error("删除区域信息失败!!!");
    }
}
